package com.google.code.drift.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.code.drift.R;
import com.google.code.drift.api.AppSkyApp;
import com.google.code.drift.api.Refreshable;
import com.google.code.drift.utils.ImageUtils;
import com.google.code.drift.utils.PreferencesProxy;
import com.google.code.drift.utils.PreferencesProxy.PrefKey;

public class ProfileTabActivity extends Activity implements Refreshable {

	AppSkyApp app;

	TextView nickText;
	ImageView sexImage;
	Button editProfileButton;
	ImageView avatarImage;

	TextView autoRefreshButton;
	TextView playChatRingButton;
	TextView chatVibratorButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.profile_tab_layout);

		app = (AppSkyApp) getApplication();
		initView();
	}

	@Override
	protected void onResume() {
		super.onResume();
		setView(app);
	}

	private void initView() {
		nickText = (TextView) findViewById(R.id.user_profile_nick);
		sexImage = (ImageView) findViewById(R.id.user_profile_sex);
		editProfileButton = (Button) findViewById(R.id.edit_user_profile);
		avatarImage = (ImageView) findViewById(R.id.user_profile_avatar);
		autoRefreshButton = (TextView) findViewById(R.id.set_auto_refresh);
		playChatRingButton = (TextView) findViewById(R.id.set_play_chat_ring);
		chatVibratorButton = (TextView) findViewById(R.id.set_chat_vibrator);

		editProfileButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ProfileTabActivity.this,
						EditUserInfoActivity.class);
				startActivity(intent);
			}
		});
		autoRefreshButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				boolean state = !app.isAutoRefresh();
				app.setAutoRefresh(state);
				PreferencesProxy.putBoolean(PrefKey.AUTO_REFRESH, state);
				setOnOffButton(state, autoRefreshButton);
			}
		});
		playChatRingButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				boolean state = !app.isChatRing();
				app.setChatRing(state);
				PreferencesProxy.putBoolean(PrefKey.PLAY_CHAT_RING, state);
				setOnOffButton(state, playChatRingButton);
			}
		});
		chatVibratorButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				boolean state = !app.isChatVibrator();
				app.setChatVibrator(state);
				PreferencesProxy.putBoolean(PrefKey.CHAT_VIBRATOR, state);
				setOnOffButton(state, chatVibratorButton);
			}
		});
	}

	private void setView(AppSkyApp app) {
		nickText.setText(app.getMyInfo().getNick());

		if (app.getMyInfo().getSex() == 0) {// 女
			sexImage.setImageResource(R.drawable.female);
		} else if (app.getMyInfo().getSex() == 1) {// 男
			sexImage.setImageResource(R.drawable.male);
		} else {// 保密
			sexImage.setImageResource(R.drawable.secrecy);
		}
		Bitmap avatar = BitmapFactory.decodeResource(getResources(),
				R.drawable.a_avatar);
		avatarImage.setImageBitmap(ImageUtils.getRoundedCornerBitmap(avatar));

		setOnOffButton(app.isAutoRefresh(), autoRefreshButton);
		setOnOffButton(app.isChatRing(), playChatRingButton);
		setOnOffButton(app.isChatVibrator(), chatVibratorButton);
	}

	/**
	 * 设置一个按钮图片的状态
	 * 
	 * @param on
	 *            如果是true则显示开，否则显示关
	 * @param button
	 */
	private void setOnOffButton(boolean on, TextView button) {

		if (on) {
			button.setCompoundDrawablesWithIntrinsicBounds(0, 0,
					R.drawable.btn_on, 0);
		} else {
			button.setCompoundDrawablesWithIntrinsicBounds(0, 0,
					R.drawable.btn_off, 0);
		}
	}

	@Override
	public void refresh() {
		
	}

}
